package com.zhaojonhy.zhaopangwb.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.zhaojonhy.zhaopangwb.R;


import java.util.ArrayList;

/**
 * Created by Administrator on 2016/9/27.
 */

public class WriteStatusGridImgsAdapter extends BaseAdapter {

    private Context context ;
    private ArrayList<Uri> imageUris ;
    private GridView gridView ;

    public WriteStatusGridImgsAdapter(Context context ,
            ArrayList<Uri> imageUris , GridView gridView ) {
        this.context = context ;
        this.imageUris = imageUris ;
        this.gridView = gridView ;
    }

    @Override
    public int getCount() {
        return imageUris.size() > 0 ? imageUris.size() + 1 : 0 ; //这里+1是给加号图片留位置
    }

    @Override
    public Uri getItem(int position) {
        return imageUris.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = null ;
        if( convertView == null ){
            holder =new Holder() ;
            convertView = View.inflate(context, R.layout.item_grid_image,null) ;
            holder.iv_image = (ImageView)convertView.findViewById(R.id.iv_image) ;
            holder.iv_delete_image = (ImageView) convertView.findViewById(R.id.iv_delete_image);
            convertView.setTag(holder);
        }else {
            holder = (Holder)convertView.getTag();
        }

        //计算宽高
        //item间的水平间距
        int horizontalsPacing = gridView.getHorizontalSpacing() ;
        int numColumns = gridView.getNumColumns() ;
        // 每一个 Item 的(总宽度-总的水平间距-左右的间距) / 列数 -- 因为做九宫格所以列数直接取3
        int itemWidth = ( gridView.getWidth() - 2*horizontalsPacing
                - gridView.getPaddingLeft() - gridView.getPaddingRight() ) / 3 ;

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth,itemWidth) ;
        //把宽高设置上imageView
        holder.iv_image.setLayoutParams(params);

        //item的位置小于总大小
        if( position < getCount() - 1) {

            //键入图片位置显示图片
            holder.iv_image.setImageURI( getItem(position)) ;
            //显示删除按钮
            holder.iv_delete_image.setVisibility(View.VISIBLE);
            //执行删除动作
            holder.iv_delete_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //移除集合数据
                    imageUris.remove(position) ;
                    //更新item列表
                    notifyDataSetChanged() ;
                }
            });
        }else { //最后一个位置，显示加号图片
            holder.iv_image.setImageResource(R.drawable.compose_pic_add_more);
            //显示删除按钮
            holder.iv_delete_image.setVisibility(View.GONE);
        }


        return convertView;
    }


    public static class Holder {
        public ImageView iv_image;
        public ImageView iv_delete_image;
    }
}
